package com.aco.party.web.controller.system;

import com.aco.party.enums.MetaTypeEnum;
import com.aco.party.model.po.Meta;
import com.aco.party.model.po.Product;
import com.aco.party.model.vo.ProductParam;
import com.aco.party.model.vo.ProductVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.MetaService;
import com.aco.party.service.system.ProductService;
import com.aco.party.web.command.system.EditProductCommand;
import com.aco.party.web.validator.system.EditProductValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import static com.aco.party.util.WebModelUtil.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageProductController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageProductController.class);

    @Resource
    private ProductService productService;

    @Resource
    private MetaService metaService;

    private EditProductValidator validator = new EditProductValidator();

    @RequestMapping(value = {"/product", "/product/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageProducts";
    }

    @RequestMapping(value = "product/list", method = RequestMethod.POST)
    @ResponseBody
    public Page <ProductVo> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <ProductVo> page = new Page<>();
        entry.webParamConvert();
        List <ProductVo> list = productService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),new ProductParam());
        page.setTotal(productService.getTotal(new ProductParam()));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/product/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response, @ModelAttribute EditProductCommand command, Model model) {
        if (command.getId() != null) {
            Product po = productService.getByPrimaryKey(command.getId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
        }
        setAtt(model);
        return "system/editProduct";
    }

    @RequestMapping(value = "/product/edit", method = RequestMethod.POST)
    public String edit(HttpServletResponse response,Model model, @ModelAttribute EditProductCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Product product = new Product();
            command.toPo(product);
            product.setUpdator(getCurrentUser().getId());
            productService.update(product);
            model.addAttribute("no_error",true);
        }
        return "system/editProduct";
    }

    @RequestMapping(value = "/product/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditProductCommand command,Model model) {
        command.setStatus(1);
        command.setType(1);
        setAtt(model);
        return "system/editProduct";
    }

    @RequestMapping(value = "/product/add", method = RequestMethod.POST)
    public String add(HttpServletResponse response,Model model,HttpSession session, @ModelAttribute EditProductCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editProduct";
        }
        Product product = new Product();
        command.toPo(product);
        product.setCreator(getCurrentUser().getId());
        productService.add(product);
        attributes.addAttribute("id",product.getId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/product/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id) {
        logger.info("删除：" + id);
        productService.delete(id);
        return new Response();
    }

    private void setAtt(Model model){
        model.addAttribute("types", convert2Pairs(metaService.getByType(MetaTypeEnum.产品类型.getValue())));
        model.addAttribute("platforms", convert2Pairs(metaService.getByType(MetaTypeEnum.平台类型.getValue())));
    }

}
